package com.apt.action;

import org.apache.struts2.dispatcher.SessionMap;

import com.apt.common.AptException;
import com.apt.facade.DataStoreHelper;
import com.apt.model.LoginDetails;

public class ChangePasswordAction extends AptBaseAction {

	private String userId;
	private String oldPwd;
	private String newPwd1;
	private String newPwd2;

	public String changePassword() {
		try {
			
			LoginDetails login= DataStoreHelper.validateUserAndPwd(userId,oldPwd);
			Integer roleId =login.getRoleId();
					
			if (roleId == null) {
				addActionError(getText("error.login"));
				return "error";
			}
			if(!newPwd1.equals(newPwd2))
			{
				addActionError(getText("error.newpasswordmatch"));
				return "error";
				
			}
			int update = DataStoreHelper.updatePwd(userId, newPwd1);
			if (session instanceof SessionMap) {

				((SessionMap) session).invalidate();

			}
			if (update == 0) {
				addActionError(getText("error.userId"));
				return "error";

			}

		} catch (AptException apt) {
			addActionError(apt.getMessage());
			return "error";

		}

		return "success";

	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public String getNewPwd1() {
		return newPwd1;
	}

	public void setNewPwd1(String newPwd1) {
		this.newPwd1 = newPwd1;
	}

	public String getNewPwd2() {
		return newPwd2;
	}

	public void setNewPwd2(String newPwd2) {
		this.newPwd2 = newPwd2;
	}

}
